package com.jxmcloud.business.cut.service.impl.database;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxmcloud.business.cut.mapper.database.IUserProgressLogDBMapper;
import com.jxmcloud.business.cut.model.database.DBUserProgressLog;
import com.jxmcloud.business.cut.service.database.IUserProgressLogDBService;
import com.jxmcloud.business.cut.vo.toDb.ToDbUserProgressLog;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserProgressLogDBServiceImpl extends ServiceImpl<IUserProgressLogDBMapper, DBUserProgressLog> implements IUserProgressLogDBService {
    @Resource
    private IUserProgressLogDBMapper userProgressLogDBMapper;

    @Override
    public void insertUserProgressLog(PlayerInfoVo player) {
        Integer activityId = player.getActivity().getActivityId();
        Integer stepId = player.getFlowInfo().getStepId();
        Integer parentStepId = player.getFlowInfo().getPreId();
        ToDbUserProgressLog log = ToDbUserProgressLog.builder()
                .uid(player.getBase().getUid())
                .activityId(activityId)
                .stepId(stepId)
                .parentStepId(parentStepId)
                .build();
        userProgressLogDBMapper.insertIgnore(log);
    }

    @Override
    public List<DBUserProgressLog> getUserProgressLogByStepId(Integer uid, Integer activityId, Integer progressId) {
        QueryWrapper<DBUserProgressLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("activity_id", activityId);
        if(progressId != null) {
            queryWrapper.eq("step_id", progressId);
        }
        queryWrapper.orderByDesc("id");
        return this.baseMapper.selectList(queryWrapper);
    }
}
